﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ListHotels : System.Web.UI.Page
{
    PagedDataSource _PageDataSource = new PagedDataSource();
    private const int pageSize = 4;
    private static double putRoom;
    private static DateTime departDate, returnDate;

    #region Private Properties
    private int CurrentPage
    {
        get
        {
            object objPage = ViewState["_CurrentPage"];
            int _CurrentPage = 0;
            if (objPage == null)
            {
                _CurrentPage = 0;
            }
            else
            {
                _CurrentPage = (int)objPage;
            }
            return _CurrentPage;
        }
        set { ViewState["_CurrentPage"] = value; }
    }
    private int fistIndex
    {
        get
        {
            int _FirstIndex = 0;
            if (ViewState["_FirstIndex"] == null)
            {
                _FirstIndex = 0;
            }
            else
            {
                _FirstIndex = Convert.ToInt32(ViewState["_FirstIndex"]);
            }
            return _FirstIndex;
        }
        set { ViewState["_FirstIndex"] = value; }
    }
    private int lastIndex
    {
        get
        {
            int _LastIndex = 0;
            if (ViewState["_LastIndex"] == null)
            {
                _LastIndex = 0;
            }
            else
            {
                _LastIndex = Convert.ToInt32(ViewState["_LastIndex"]);
            }
            return _LastIndex;
        }
        set { ViewState["_LastIndex"] = value; }
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }

    private void LoadData()
    {
        //Get departDate for search
        ddlDayDepart.SelectedValue = DateTime.Now.Day.ToString();
        ddlMonthDepart.SelectedValue = DateTime.Now.Month.ToString();
        GetSearchCondition();
        
        //Get list hotel
        DataTable dtHotels = HotelManagement.GetAllHotelFromPR(HotelManagement.GetAllHotel().Tables[0]).Tables[0];

        BindData(dtHotels);
    }

    private void BindData(DataTable dtHotels)
    {
        DataTable dtNewHotels = CopyData(dtHotels);
        _PageDataSource.DataSource = dtNewHotels.DefaultView;
        _PageDataSource.AllowPaging = true;
        _PageDataSource.PageSize = pageSize;
        _PageDataSource.CurrentPageIndex = CurrentPage;
        ViewState["TotalPages"] = _PageDataSource.PageCount;

        this.lblPageInfo.Text = "Page " + (CurrentPage + 1) + " of " + _PageDataSource.PageCount;
        this.lbtnPrevious.Enabled = !_PageDataSource.IsFirstPage;
        this.lbtnNext.Enabled = !_PageDataSource.IsLastPage;
        this.lbtnFirst.Enabled = !_PageDataSource.IsFirstPage;
        this.lbtnLast.Enabled = !_PageDataSource.IsLastPage;

        DataList3.DataSource = _PageDataSource;
        DataList3.DataBind();
        this.doPaging();
    }

    private void GetSearchCondition()
    {
        departDate = new DateTime(DateTime.Now.Year, int.Parse(ddlMonthDepart.SelectedValue), int.Parse(ddlDayDepart.SelectedValue));
        returnDate = new DateTime(DateTime.Now.Year, int.Parse(ddlMonthDepart.SelectedValue), int.Parse(ddlDayDepart.SelectedValue) + int.Parse(ddlNight.SelectedValue));

        double numAdult = double.Parse(ddlAdult.SelectedValue);
        double numChild = double.Parse(ddlChild.SelectedValue);
        double calRoom = Math.Ceiling(numAdult / Common.ADULT_PER_ROOM + numChild / Common.CHILD_PER_ROOM);
        putRoom = calRoom > double.Parse(ddlRoom.SelectedValue) ? calRoom : double.Parse(ddlRoom.SelectedValue);
        ddlRoom.SelectedValue = putRoom.ToString();
    }

    private void doPaging()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("PageIndex");
        dt.Columns.Add("PageText");
        fistIndex = CurrentPage - (pageSize+1);
        if (CurrentPage > pageSize + 1)
        {
            lastIndex = CurrentPage + (pageSize + 1);
        }
        else
        {
            lastIndex = 2 * (pageSize + 1);
        }
        if (lastIndex > Convert.ToInt32(ViewState["TotalPages"]))
        {
            lastIndex = Convert.ToInt32(ViewState["TotalPages"]);
            fistIndex = lastIndex - 2 * (pageSize + 1);
        }
        if (fistIndex < 0)
        {
            fistIndex = 0;
        }
        for (int i = fistIndex; i < lastIndex; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = i;
            dr[1] = i + 1;
            dt.Rows.Add(dr);
        }
        this.dlPaging.DataSource = dt;
        this.dlPaging.DataBind();
    }
    protected void lbtnNext_Click(object sender, EventArgs e)
    {
        CurrentPage += 1;
        this.LoadData();
    }
    protected void lbtnPrevious_Click(object sender, EventArgs e)
    {
        CurrentPage -= 1;
        this.LoadData();
    }
    protected void lbtnLast_Click(object sender, EventArgs e)
    {
        CurrentPage = (Convert.ToInt32(ViewState["TotalPages"]) - 1);
        this.LoadData();
    }
    protected void lbtnFirst_Click(object sender, EventArgs e)
    {

        CurrentPage = 0;
        this.LoadData();
    }


    protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.Equals("Paging"))
        {
            CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
            this.LoadData();
        }
    }
    protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
        if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
        {
            lnkbtnPage.Enabled = false;
            lnkbtnPage.Style.Add("fone-size", "14px");
            lnkbtnPage.Font.Bold = true;

        }
    }


    private static DataTable CopyData(DataTable dtHotels)
    {
        DataTable dtNewHotels = new DataTable();
        //DataColumn newColumn1 = new DataColumn("stt");
        DataColumn newColumn2 = new DataColumn(HotelManagement.HOTELID);
        DataColumn newColumn3 = new DataColumn(HotelManagement.HOTELNAME);
        DataColumn newColumn4 = new DataColumn(HotelManagement.PRICE);
        DataColumn newColumn5 = new DataColumn(HotelManagement.START);
        DataColumn newColumn6 = new DataColumn(HotelManagement.ADDRESS);
        DataColumn newColumn7 = new DataColumn(HotelManagement.PHONE);
        DataColumn newColumn8 = new DataColumn(HotelManagement.HOTELDESCRIPTION);
        DataColumn newColumn9 = new DataColumn(ImageManagement.PATHLINK);
        //dtNewHotels.Columns.Add(newColumn1);
        dtNewHotels.Columns.Add(newColumn2);
        dtNewHotels.Columns.Add(newColumn3);
        dtNewHotels.Columns.Add(newColumn4);
        dtNewHotels.Columns.Add(newColumn5);
        dtNewHotels.Columns.Add(newColumn6);
        dtNewHotels.Columns.Add(newColumn7);
        dtNewHotels.Columns.Add(newColumn8);
        dtNewHotels.Columns.Add(newColumn9);

        DataRow row;
        for (int i = 0; i < dtHotels.Rows.Count; i++)
        {
            row = dtNewHotels.NewRow();

            //row[0] = dtHotels.Rows[i][0];
            row[0] = dtHotels.Rows[i][HotelManagement.HOTELID];
            row[1] = dtHotels.Rows[i][HotelManagement.HOTELNAME];
            row[2] = dtHotels.Rows[i][HotelManagement.PRICE];
            switch (dtHotels.Rows[i][HotelManagement.START].ToString())
            {
                case "1":
                    row[3] = "img/1star.png";
                    break;
                case "2":
                    row[3] = "img/2star.png";
                    break;
                case "3":
                    row[3] = "img/3star.png";
                    break;
                case "4":
                    row[3] = "img/4star.png";
                    break;
                case "5":
                    row[3] = "img/5star.png";
                    break;
            }
            row[4] = dtHotels.Rows[i][HotelManagement.ADDRESS];
            row[5] = dtHotels.Rows[i][HotelManagement.PHONE];
            if (dtHotels.Rows[i][HotelManagement.HOTELDESCRIPTION] != null && dtHotels.Rows[i][HotelManagement.HOTELDESCRIPTION].ToString().Length > 0)
            {
                string[] temp = dtHotels.Rows[i][HotelManagement.HOTELDESCRIPTION].ToString().Split('.');
                if (temp.Length == 1)
                    row[6] = temp[0] + " ...";
                else
                    row[6] = temp[0] + ". " + temp[1] + " ...";
            }
            else
                row[6] = null;
            row[7] = dtHotels.Rows[i][ImageManagement.PATHLINK];

            dtNewHotels.Rows.Add(row);
        }
        return dtNewHotels;
    }
    protected void btnCheckRates_Click(object sender, ImageClickEventArgs e)
    {
        //Search room
        DataTable dtSearchHotels = HotelManagement.SearchHotel(Util.ConvertDateToString(departDate),Util.ConvertDateToString(returnDate), int.Parse(ddlRoom.SelectedValue)).Tables[0];
        DataTable dtHotels = HotelManagement.GetAllHotelFromPR(dtSearchHotels).Tables[0];
        BindData(dtHotels);
    }
    protected void lblHotelName_Click(object sender, EventArgs e)
    {
        LinkButton myButton = sender as LinkButton;
        if (myButton != null)
        {
            int hotelID = int.Parse(myButton.CommandArgument);
            Response.Redirect("DetailHotel.aspx?hotelID=" + hotelID);
        }
    }

    protected void ddlDayDepart_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetSearchCondition();
    }
    protected void ddlMonthDepart_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetSearchCondition();
    }
    protected void ddlNight_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetSearchCondition();
    }
    protected void ddlRoom_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetSearchCondition();
    }
    protected void ddlAdult_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetSearchCondition();
    }
    protected void ddlChild_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetSearchCondition();
    }
}